﻿/*=================================================================
	TASK 9:                                                 {Arrays}
	Write a program that finds the most frequent number in an array.
    Example:{4, 1, 1, 4, 2, 3, 4, 4, 1, 2, 4, 9, 3} => 4 (5 times)
==================================================================*/

using System;

class MostFrequentNumber
{
    static void Main()
    {
        //int[] arr = { 4, 1, 1, 4, 2, 3, 1, 4, 4, 1, 2, 1, 4, 9 };

        int[] arr = { 4,2, 1, 4,2, 4, 2};

        int mostFrequent = 0;
        string finalFrequent = "";
        int frequent = 0;
        int currentNumber;
        int repeats = 0;
        int maxRepeats = 0;

        for (int i = 0; i < arr.Length; i++)
        {
            currentNumber = arr[i];
            if (currentNumber == mostFrequent && i != 0)
            {
                continue;
            }
            for (int j = 0; j < arr.Length; j++)
            {
                if (currentNumber == arr[j])
                {
                    frequent = currentNumber;
                    repeats++;
                }
            }

            if (repeats > maxRepeats)
            {
                mostFrequent = frequent;
                maxRepeats = repeats;
            }

            repeats = 0;
        }

        if (maxRepeats > 1)
        {
            Console.WriteLine(finalFrequent);
            Console.WriteLine("Most frequent number = {0}", mostFrequent);
            Console.WriteLine("Count = {0} times", maxRepeats);
        }
        else
        {
            Console.WriteLine("All elements are different");
        }
    }
}